package com.demo.api.conf;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * @author: cja
 * @date: 2019/11/13 20:03
 */
@Data
public class ResultT<T> {
    private int code;
    private String msg;
    private T data;

    public static final int SUCCESS_CODE = 200;//成功
    public static final int AUTH_ERROR_CODE = 300;//权限错误
    public static final int CLIENT_ERROR_CODE = 400;//客户端错误
    public static final int SERVER_ERROR_CODE = 500;//服务端错误

    public ResultT() {
        this.code = SUCCESS_CODE;
        this.msg = "请求成功";
    }

    public ResultT(T data) {
        this.code = SUCCESS_CODE;
        this.msg = "请求成功";
        this.data = data;
    }

    public ResultT(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ResultT error(int code, String msg){
        return new ResultT(code == 0 ? SERVER_ERROR_CODE : code,
                    StringUtils.isEmpty(msg) ? "未知错误" : msg);
    }
}
